package com.example.mysqltry.service.impl;

import com.example.mysqltry.api.vo.NewBeeMallSearchGoodsVO;
import com.example.mysqltry.dao.MallGoodsInfoMapper;
import com.example.mysqltry.entity.MallGoodsInfo;
import com.example.mysqltry.service.NewBeeMallGoodsService;
import com.example.mysqltry.util.BeanUtil;
import com.example.mysqltry.util.PageQueryUtil;
import com.example.mysqltry.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class NewBeeMallGoodsServiceImpl implements NewBeeMallGoodsService {
    @Autowired
    private MallGoodsInfoMapper mallGoodsInfoMapper;
    @Override
   public MallGoodsInfo getNewBeeMallGoodsById(Long id)
    {

      MallGoodsInfo mallGoodsInfo= mallGoodsInfoMapper.selectByPrimaryKey(id);



        return mallGoodsInfo;
    }
    @Override
    public PageResult searchNewBeeMallGoods(PageQueryUtil pageUtil){
        List<MallGoodsInfo> mallGoodsInfos= mallGoodsInfoMapper.findNewBeeMallGoodsListBySearch(pageUtil);
        int total =mallGoodsInfoMapper.getTotalNewBeeMallGoodsBySearch(pageUtil);
        List<NewBeeMallSearchGoodsVO>  newBeeMallSearchGoodsVOS=new ArrayList<>();
        if(!CollectionUtils.isEmpty(mallGoodsInfos))
        {
            newBeeMallSearchGoodsVOS= BeanUtil.copyList(mallGoodsInfos,NewBeeMallSearchGoodsVO.class);
            for(NewBeeMallSearchGoodsVO newBeeMallSearchGoodsVO:newBeeMallSearchGoodsVOS)

            {
                String intro=newBeeMallSearchGoodsVO.getGoodsIntro();
                String name=newBeeMallSearchGoodsVO.getGoodsName();
                if(intro.length()>30)
                {
                    String newintro=intro.substring(0,30)+"...";
                    newBeeMallSearchGoodsVO.setGoodsIntro(newintro);
                }

                if(name.length()>30)
                {
                    String newname=name.substring(0,30)+"...";
                    newBeeMallSearchGoodsVO.setGoodsName(newname);
                }


            }

        }
        PageResult pageResult=new PageResult(newBeeMallSearchGoodsVOS,total,pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }

}
